package com.weetop.aklyscan.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.weetop.aklyscan.R;

import java.io.IOException;

import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class ChiCunDialog extends Dialog {
    private Context mContext;
    private ImageView back;
    private EditText chang, kuan, gao;
    private TextView chicun, button;

    public ChiCunDialog(Context context, TextView chicun) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_chicun);
        mContext = context;
        this.chicun = chicun;
        initVar();
        initView();
        initAttr();
        initAction();
    }

    public void initVar() {
    }

    public void initView() {
        back = findViewById(R.id.back);
        chang = findViewById(R.id.chang);
        kuan = findViewById(R.id.kuan);
        gao = findViewById(R.id.gao);
        button = findViewById(R.id.button);
    }

    public void initAttr() {
        chang.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    playRing("chang.mp3");
                }
            }
        });
        kuan.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    playRing("kuan.mp3");
                }
            }
        });
        gao.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    playRing("gao.mp3");
                }
            }
        });
    }

    public void initAction() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(chang.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(mContext, "长度不能为空");
                    return;
                }
                if (TextUtils.isEmpty(kuan.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(mContext, "宽度不能为空");
                    return;
                }
                if (TextUtils.isEmpty(gao.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(mContext, "高度不能为空");
                    return;
                }
                chicun.setText(chang.getText().toString().trim() + "*" + kuan.getText().toString().trim() + "*" +
                        gao.getText().toString().trim());
                dismiss();
            }
        });
    }

    private AssetManager assetManager;
    private MediaPlayer player = new MediaPlayer();

    private void playRing(String fileName) {
        try {
            assetManager = mContext.getAssets();
            AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
//            if (player.isPlaying()) {
            player.reset();
//            }
            player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
                    fileDescriptor.getLength());
            player.prepare();
            player.start();
            player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    player.stop();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        if (player.isPlaying()) {
            player.stop();
        }
        player.release();
        super.onStop();
    }
}
